package com.exam.util;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;



/**
 * 
 * author:刘楠
 */
public class ExcelExportUtils<T> {
	
	
	private String[] headers = {"学号","班级","姓名","考试科目","成绩"};

	public void generateExcel(String sheetName, String headerTitle,
			List<T> t, OutputStream os, String datePattern) {
		try {
			// 创建一个工作空间
			Workbook workbook = new HSSFWorkbook();
			// 创建一个工作薄
			Sheet sheet = workbook.createSheet(sheetName);
			sheet.setDefaultColumnWidth(18);
			// 合并单元格
			sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 7));

			Row row = sheet.createRow(0);
			Cell cell = row.createCell(0);

			CellStyle style = workbook.createCellStyle();

			style.setAlignment(HorizontalAlignment.CENTER);
			Font font = workbook.createFont();
			font.setFontHeightInPoints((short) 18);
			style.setFont(font);
			style.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
		    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			cell.setCellStyle(style);
			cell.setCellValue(headerTitle);

			// 创建一级标题
			Row row1 = sheet.createRow(2);
			for (int i = 0; i < headers.length; i++) {
				Cell createCell = row1.createCell(i);
				createCell.setCellValue(headers[i]);
			}
			// 创建数据行
			if(t!=null && t.size()!=0){
				Field[] declaredFields = t.get(0).getClass().getDeclaredFields();
				for (int i = 0; i < t.size(); i++) {
					Row rowTemp = sheet.createRow(i + 3);
					for (int j = 0; j < headers.length; j++) {
						Cell cellTemp = rowTemp.createCell(j);
						CellStyle dataStyle = workbook.createCellStyle();
						dataStyle.setAlignment(HorizontalAlignment.CENTER);
						cellTemp.setCellStyle(dataStyle);
						String name = declaredFields[j].getName();
						String getMethodName = "get"
								+ name.substring(0, 1).toUpperCase()
								+ name.substring(1);
						Class<?> type = declaredFields[j].getType();
						Method getMethod = t.get(i).getClass().getDeclaredMethod(
								getMethodName, new Class[] {});
						Object invoke = getMethod.invoke(t.get(i),
								new Object[] {});
						if (type == Integer.class || type == String.class
								|| type == Double.class || type == double.class || type == Float.class) {
							cellTemp.setCellValue(invoke.toString());
						}
						if (type == Date.class) {
							if (invoke != null) {
								cellTemp.setCellValue(new SimpleDateFormat(
										datePattern).format(invoke));
							}
						}
					}
				}
				// 写出excel
				workbook.write(os);
				os.close();
			}
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
